package com.etc.controller;

import com.etc.entity.Building;
import com.etc.entity.CommentResult;
import com.etc.service.BuildingService;
import com.etc.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    @Autowired
    private ParkService parkService;

    @RequestMapping("building/findAllBuilding")
    public CommentResult findAllBuilding(@RequestParam(value = "start", required = false) Integer start,
                                         @RequestParam(value = "size", required = false) Integer size){
        List<Building> list = buildingService.findAllBuilding();
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("name",list.get(i).getName());
            map.put("layers",list.get(i).getLayers());
            map.put("style",list.get(i).getStyle());
            map.put("area",list.get(i).getArea());
            result.add(map);
        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("building/findByAnyCondition")
    public CommentResult findByAnyCondition(@RequestBody Map<String, Object> map, @RequestParam(value = "start", required = false) Integer start,
                                            @RequestParam(value = "size", required = false) Integer size){

        List<Building> list = buildingService.findByAnyCondition(map);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map1= new HashMap<>();
            map1.put("id",list.get(i).getId());
            map1.put("park",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map1.put("park_id",parkService.findById(list.get(0).getPark_id()).getId());
            map1.put("name",list.get(i).getName());
            map1.put("layers",list.get(i).getLayers());
            map1.put("style",list.get(i).getStyle());
            map1.put("area",list.get(i).getArea());
            result.add(map1);
        }
        return new CommentResult(200,"success",result);
    }
    @RequestMapping("building/findById/{id}")
    public CommentResult findById(@PathVariable("id")Integer id)
    {
        Building result = buildingService.findById(id);
        List<Map<String,Object>> result1 = new ArrayList<>();

        Map<String,Object> map = new HashMap<>();
        map.put("id",result.getId());
        map.put("park",parkService.findById(result.getPark_id()).getPark_name());
        map.put("park_id",parkService.findById(result.getPark_id()).getId());
        map.put("name",result.getName());
        map.put("layers",result.getLayers());
        map.put("style",result.getStyle());
        map.put("area",result.getArea());
        result1.add(map);

        return new CommentResult(200,"success",result1);
    }
    @RequestMapping("building/findByPark/{id}")
    public CommentResult findByPark(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size)
    {

        List<Building> list = buildingService.findByPark(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park",parkService.findById(list.get(0).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(0).getPark_id()).getId());
            map.put("name",list.get(i).getName());
            map.put("layers",list.get(i).getLayers());
            map.put("style",list.get(i).getStyle());
            map.put("area",list.get(i).getArea());
            result.add(map);
        }
        return new CommentResult(200,"success",result);
    }
}


